using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.MainManagers
{
	#region using
	using DevComponents.DotNetBar;
	using AppManagement.Models;
	using AppManagement.Repositories;
	using AppManagement.Helpers;
	using System.Data;
	using System.Drawing;
	using System.IO;
	#endregion

	public partial class FrmTimKiemSinhVien : Office2007Form
	{
		public EventHandler Handler { get; set; }
		private DevComponents.DotNetBar.Controls.TextBoxX txtMaKhoa;
		private DevComponents.DotNetBar.Controls.TextBoxX txtMaHDT;
		private DevComponents.DotNetBar.Controls.TextBoxX txtMaKH;

        public FrmTimKiemSinhVien()
		{
			InitializeComponent();
		}

        private void FrmQuanLySinhVien_Load(object sender, EventArgs e)
        {
            InitGridView();
            InitHiddenFields();
            dtgvData.ClearSelection();
        }

		private void InitGridView()
		{
			DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "MSSV",
				DataPropertyName = "MSSV",
				Name = "MSSV",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5))
			};
			dtgvData.Columns.Add(objColumn);
            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "Họ Tên",
                DataPropertyName = "HoTen",
                Name = "HoTen",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 5))
            };
            dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Chuyên ngành",
				DataPropertyName = "TenCN",
                Name = "TenCN",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Hệ đào tạo",
				DataPropertyName = "TenHDT",
                Name = "TenHDT",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Khóa học",
				DataPropertyName = "TenKH",
                Name = "TenKH",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5)) - 2
			};
			dtgvData.Columns.Add(objColumn);
		}

		public void InitHiddenFields()
		{
            txtMaKhoa = new DevComponents.DotNetBar.Controls.TextBoxX
			{
				Name = "txtMaKhoa",
				Text = string.Empty
			};
            txtMaKhoa.Visible = false;
			txtMaHDT = new DevComponents.DotNetBar.Controls.TextBoxX
			{
				Name = "txtMaHDT",
				Text = string.Empty
			};
			txtMaHDT.Visible = false;
			txtMaKH = new DevComponents.DotNetBar.Controls.TextBoxX
			{
				Name = "txtMaKH",
				Text = string.Empty
			};
			txtMaKH.Visible = false;
		}

		public void TransferDataInfo(object sender, TransferSimpleDataEvent e)
		{
			this.Close();
			this.Handler(this, e);
		}

		private void btnChonChuyenNganh_Click(object sender, EventArgs e)
		{
            FrmQuanLyKhoa frm = new FrmQuanLyKhoa(true);
            frm.Handler += GetKhoa;
            frm.ShowDialog();
		}

        public void GetKhoa(object sender, EventArgs e)
        {
            var eventType = (TransferSimpleDataEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });
            txtMaKhoa.Text = comp[0];
            txtKhoa.Text = comp[1];

            var lstChuyenNganh = ChuyenNganhRepository.SelectByMaKhoa(int.Parse(txtMaKhoa.Text));
            cbxChuyenNganh.Items.Clear();
            foreach (var item in lstChuyenNganh)
            {
                cbxChuyenNganh.Items.Add(item);
            }
        }

		private void btnChonHeDaoTao_Click(object sender, EventArgs e)
		{
			FrmQuanLyHeDaoTao frm = new FrmQuanLyHeDaoTao(true);
			frm.Handler += GetHeDaoTao;
			frm.ShowDialog();
		}

		public void GetHeDaoTao(object sender, EventArgs e)
		{
			var eventType = (TransferSimpleDataEvent)e;
			string[] comp = eventType.Data.Split(new char[] { '#' });
			txtMaHDT.Text = comp[0];
			txtHeDaoTao.Text = comp[1];
		}

		private void btnChonKhoaHoc_Click(object sender, EventArgs e)
		{
			FrmQuanLyKhoaHoc frm = new FrmQuanLyKhoaHoc(true);
			frm.Handler += GetKhoaHoc;
			frm.ShowDialog();
		}

		public void GetKhoaHoc(object sender, EventArgs e)
		{
			var eventType = (TransferSimpleDataEvent)e;
			string[] comp = eventType.Data.Split(new char[] { '#' });
			txtMaKH.Text = comp[0];
			txtKhoaHoc.Text = comp[1];
		}

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXemDiemHocTap_Click(object sender, EventArgs e)
        {
            if (dtgvData.SelectedRows.Count > 0)
            {
                FrmDiemTongHopSV frm = new FrmDiemTongHopSV(dtgvData.SelectedRows[0].Cells["MSSV"].Value.ToString());
                frm.ShowDialog();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            var chuyennganh = string.Empty;
            if (cbxChuyenNganh.SelectedIndex != -1)
            {
                chuyennganh = ((ChuyenNganh)cbxChuyenNganh.SelectedItem).MaCN.ToString();
            }
            var lstItem = SinhVienRepository.TimKiem(txtMSSV.Text, txtMaKhoa.Text, chuyennganh, txtMaHDT.Text, txtMaKH.Text);

            var dt = new DataTable();
            dt.Columns.Add("MSSV");
            dt.Columns.Add("HoTen");
            dt.Columns.Add("TenCN");
            dt.Columns.Add("TenHDT");
            dt.Columns.Add("TenKH");
            for (int i = 0; i < lstItem.Count; i++)
            {
                dt.Rows.Add(lstItem[i].MSSV, lstItem[i].HoTen, lstItem[i].ChuyenNganh.TenCN, lstItem[i].HeDaoTao.TenHDT, lstItem[i].KhoaHoc.TenKH);
            }
            DataView objTimeSheetDV = new System.Data.DataView(dt);
            objTimeSheetDV.AllowNew = false;
            dtgvData.AutoGenerateColumns = false;
            dtgvData.DataSource = objTimeSheetDV;
            dtgvData.ClearSelection();
        }

        private void btnResetHDT_Click(object sender, EventArgs e)
        {
            txtMaHDT.Text = string.Empty;
            txtHeDaoTao.Text = string.Empty;
        }

        private void btnResetKhoaHoc_Click(object sender, EventArgs e)
        {
            txtMaKH.Text = string.Empty;
            txtKhoaHoc.Text = string.Empty;
        }

        private void btnResetKhoa_Click(object sender, EventArgs e)
        {
            txtMaKhoa.Text = string.Empty;
            txtKhoa.Text = string.Empty;
            cbxChuyenNganh.Items.Clear();
            cbxChuyenNganh.Text = string.Empty;
        }

        private void dtgvData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dtgvData.SelectedRows.Count > 0)
			{
                FrmQuanLySinhVien frm = new FrmQuanLySinhVien(string.Empty, false, dtgvData.SelectedRows[0].Cells["MSSV"].Value.ToString());
				frm.ShowDialog();
			}

        }
	}

}
